package com.jsj.servlet.ch4;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/ShoppingListServlet")
public class ShoppingListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset = UTF-8");
		PrintWriter out = response.getWriter();
		
		HttpSession session = request.getSession();
		@SuppressWarnings("unchecked")
		Map<String,Integer>car = (Map<String,Integer>)session.getAttribute("shoppingCar");
		
		if(car!=null && car.size()>0) {
			out.println("<p>您购买的书籍有:    </p>");
			//遍历显示购物车中的书籍名称和选择次数
			for(String bookName : car.keySet()) {
				out.println("<p>" + bookName + "," + car.get(bookName) + "本</p>");
			}	
		}else {
			out.println("<p>您还为购买任何书籍！     </p>");
		}	
			out.println("<p><a href = 'bookChoose.jsp'>继续购买   </a></p>");
	}

}
